package com.study;

import com.study.dao.UserDao;
import com.study.dao.otherBean;
import com.study.mapper.UserMapper;
import com.study.pojo.User;
import com.study.service.UserService;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class PrimaryTest {
    public static void main(String[] args) {
        /*
        //获取bean的方式一：
        //创建BeanFactory
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        //创建读取器
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        //加载配置文件
        reader.loadBeanDefinitions("beans.xml");
        //获取Bean实例对象
        UserService userService = (UserService)beanFactory.getBean("userService");
        System.out.println(userService);*/
        //方式二：ApplicationContext
//        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//
//        UserService userService = (UserService)applicationContext.getBean("userService");
//        UserDao userDao = (UserDao)applicationContext.getBean("userDao");
//        System.out.println(userDao);
//        System.out.println(userService);
//        applicationContext.close();

        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Object dataSource = applicationContext.getBean("dataSource");
        System.out.println(dataSource);
        //        UserMapper userMapper = applicationContext.getBean(UserMapper.class);
//        List<User> all = userMapper.findAll();
//        System.out.println(all);
        //MapperScannerConfigurer
        //SqlSessionFactoryBean
//        UserDao userDao = (UserDao)applicationContext.getBean("userDao");
//        System.out.println(userDao);
//        userDao.func1();
//        userDao.func2();
        //        otherBean userDao = (otherBean)applicationContext.getBean("otherBean");
//        System.out.println(userDao);
//        userDao.func1();
//        userDao.func2();

    }
}
